package com.maxd.controller.helpTask;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.*;
import com.maxd.respository.userinfo.UserJpaRepository;
import com.maxd.service.helpTask.IHelpGetTaskeSrvice;
import com.maxd.service.helpTask.IHelpLuckyDetailsService;
import com.maxd.service.helpTask.IHelpLuckyValueService;
import com.maxd.service.helpTask.IHelpTaskPlatformService;
import com.maxd.service.lucky.ILuckyQueueService;
import com.maxd.service.lucky.ILuckyValueQueueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fang
 * @date 2020/7/15
 */
@RestController
@Api(value = "平台任务", tags = {"平台任务"})
@RequestMapping(value = "/helpTaskPlatform")
public class HelpTaskPlatformController {

    private final IHelpTaskPlatformService iHelpTaskPlatformService;
    private final IHelpGetTaskeSrvice iHelpGetTaskeSrvice;
    private final IHelpLuckyValueService iHelpLuckyValueService;
    private final IHelpLuckyDetailsService luckyDetailsService;
    private final ILuckyValueQueueService iLuckyValueQueueService;
    private final UserJpaRepository userJpaRepository;
    private final ILuckyQueueService luckyQueueService;

    @Autowired
    public HelpTaskPlatformController(IHelpTaskPlatformService iHelpTaskPlatformService, IHelpGetTaskeSrvice iHelpGetTaskeSrvice, IHelpLuckyValueService iHelpLuckyValueService, IHelpLuckyDetailsService luckyDetailsService, ILuckyValueQueueService iLuckyValueQueueService, UserJpaRepository userJpaRepository, ILuckyQueueService luckyQueueService) {
        this.iHelpTaskPlatformService = iHelpTaskPlatformService;
        this.iHelpGetTaskeSrvice = iHelpGetTaskeSrvice;
        this.iHelpLuckyValueService = iHelpLuckyValueService;
        this.luckyDetailsService = luckyDetailsService;
        this.iLuckyValueQueueService = iLuckyValueQueueService;
        this.userJpaRepository = userJpaRepository;
        this.luckyQueueService = luckyQueueService;
    }

    @RequestMapping(value = "/saveHelpTaskPlatform", method = RequestMethod.POST)
    @ApiOperation("新建任务")
    @ResponseBody
    public Result saveHelpTaskPlatform(@RequestBody HelpTaskPlatform helpTaskPlatform) {
        iHelpTaskPlatformService.saveBody(helpTaskPlatform);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/updateHelpTaskPlatform", method = RequestMethod.POST)
    @ApiOperation("修改任务")
    @ResponseBody
    public Result updateHelpTaskPlatform(@RequestBody HelpTaskPlatform helpTaskPlatform) {
        iHelpTaskPlatformService.updateHelpTaskPlatform(helpTaskPlatform);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteHelpTaskPlatformById", method = RequestMethod.POST)
    @ApiOperation("删除任务")
    @ResponseBody
    public Result deleteHelpTaskPlatformById(Long id) {
        iHelpTaskPlatformService.deleteHelpTaskPlatformById(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteHelpTaskPlatformByIds", method = RequestMethod.POST)
    @ApiOperation("删除多个任务")
    @ResponseBody
    public Result deleteHelpTaskPlatformByIds(String ids) {
        iHelpTaskPlatformService.deleteHelpTaskPlatformByIds(ids);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/updateHelpTaskPlatformStateById", method = RequestMethod.POST)
    @ApiOperation("修改任务状态  1正常 2下架")
    @ResponseBody
    public Result updateHelpTaskPlatformStateById(Long id,Integer state) {
        iHelpTaskPlatformService.updateHelpTaskPlatformStateById(id,state);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectHelpTaskPlatformList", method = RequestMethod.GET)
    @ApiOperation("查看所有任务")
    @ResponseBody
    public Result selectHelpTaskPlatformList(int size,int page) {
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "state");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iHelpTaskPlatformService.selectHelpTaskPlatformListByState(pageable,null));
    }

    @RequestMapping(value = "/selectHelpTaskPlatformListByClassifyId", method = RequestMethod.GET)
    @ApiOperation("根据分类查看任务")
    @ResponseBody
    public Result selectHelpTaskPlatformListByClassifyId(int size,int page,Long classifyId) {
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "state");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iHelpTaskPlatformService.selectHelpTaskPlatformList(pageable,classifyId));
    }


    @RequestMapping(value = "/selectHelpGetTaskById", method = RequestMethod.GET)
    @ApiOperation("查看任务详细信息用户端")
    @ResponseBody
    public Result selectHelpGetTaskById(Long id,Long userId) {
        return ResultUtil.success(iHelpGetTaskeSrvice.selectHelpGetTaskById(id,userId));
    }

    @RequestMapping(value = "/selectHelpTaskPlatformById", method = RequestMethod.GET)
    @ApiOperation("查看任务详细信息管理平台")
    @ResponseBody
    public Result selectHelpGetTaskById(Long id) {
        return ResultUtil.success(iHelpGetTaskeSrvice.selectHelpTaskPlatform(id));
    }

    @RequestMapping(value = "/selectHelpGetTaskListByUserId", method = RequestMethod.GET)
    @ApiOperation("查看我的任务列表")
    @ResponseBody
    public Result selectHelpGetTaskListByUserId(int size,int page,Long userId,Integer state) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iHelpGetTaskeSrvice.selectHelpGetTaskList(pageable,userId,state));
    }

    @RequestMapping(value = "/selectHelpGetTaskList", method = RequestMethod.GET)
    @ApiOperation("查看所有接单任务列表")
    @ResponseBody
    public Result selectHelpGetTaskList(int size,int page) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iHelpGetTaskeSrvice.selectHelpGetTaskList(pageable));
    }

    @RequestMapping(value = "/selectUnderwayHelpGetTaskList", method = RequestMethod.GET)
    @ApiOperation("查看进行中的接单任务")
    @ResponseBody
    public Result selectUnderwayHelpGetTaskList(int size,int page) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iHelpGetTaskeSrvice.selectHelpGetTaskList(pageable));
    }

    @RequestMapping(value = "/helpGetTaskRemind/{id}", method = RequestMethod.POST)
    @ApiOperation("对进行中的任务进行提醒")
    @ResponseBody
    public Result helpGetTaskRemind(@PathVariable  Long id) {
        iHelpGetTaskeSrvice.helpGetTaskRemind(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectUnderwayHelpGetTaskList/{id}", method = RequestMethod.POST)
    @ApiOperation("对进行中的任务进行放弃")
    @ResponseBody
    public Result exitHelpGetTask(@PathVariable  Long id) {
        iHelpGetTaskeSrvice.exitHelpGetTask(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/insertHelpGetTask", method = RequestMethod.POST)
    @ApiOperation("领取任务")
    @ResponseBody
    public Result insertHelpGetTask(Long helpTaskPlatformId,Long userId) {
        HelpTaskPlatform helpTaskPlatform = iHelpTaskPlatformService.selectById(helpTaskPlatformId);
        if(helpTaskPlatform!=null && helpTaskPlatform.getState()==1){
            Integer count = iHelpGetTaskeSrvice.selectCount(helpTaskPlatformId, userId);
            if(count!=null && count>0){
                return ResultUtil.error(-100,"不能重复领取任务！");
            }
            iHelpGetTaskeSrvice.saveBody(helpTaskPlatformId,userId);
            return ResultUtil.success();
        }else{
            return ResultUtil.error(-100,"任务已经下架！");

        }
    }


    @RequestMapping(value = "/auditHelpGetTask/{ids}/{state}/{auditContent}", method = RequestMethod.POST)
    @ApiOperation("审核任务")
    @ResponseBody
    public Result auditHelpGetTask(@PathVariable("ids") String ids,@PathVariable("state") Integer state,@PathVariable("auditContent") String auditContent) {
        iHelpGetTaskeSrvice.auditHelpGetTask(ids,state,auditContent);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/updateHelpGetTask", method = RequestMethod.POST)
    @ApiOperation("提交任务")
    @ResponseBody
    public Result updateHelpGetTask(Long id,Long userId,String picture,String content) {
        HelpGetTask helpGetTask = iHelpGetTaskeSrvice.selectById(id);
        if(helpGetTask==null){
            return ResultUtil.error(-100,"请先领取任务后再进行提交！");
        }
        if(helpGetTask.getState()!=0){
            return ResultUtil.error(-100,"任务已经做过了，不能重复提交！");
        }
        if(!helpGetTask.getUserId().equals(userId)){
            return ResultUtil.error(-100,"提交失败，请刷新后重试！");
        }
        helpGetTask.setPicture(picture);
        helpGetTask.setContent(content);
        helpGetTask.setState(1);
        iHelpGetTaskeSrvice.updateHelpGetTaskById(helpGetTask);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/giveUpHelpGetTask", method = RequestMethod.POST)
    @ApiOperation("放弃任务")
    @ResponseBody
    public Result giveUpHelpGetTask(Long id,Long userId) {
        HelpGetTask helpGetTask = iHelpGetTaskeSrvice.selectById(id);
        if(helpGetTask==null){
            return ResultUtil.error(-100,"请先领取任务后再进行提交！");
        }
        if(helpGetTask.getState()!=0){
            return ResultUtil.error(-100,"任务已经做过了，不能放弃！");
        }
        if(!helpGetTask.getUserId().equals(userId)){
            return ResultUtil.error(-100,"提交失败，请刷新后重试！");
        }
        helpGetTask.setState(4);
        iHelpGetTaskeSrvice.updateHelpGetTaskById(helpGetTask);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/selectLuckyValue", method = RequestMethod.GET)
    @ApiOperation("查看我的幸运值")
    @ResponseBody
    public Result selectLuckyValue(Long userId) {
        HelpLuckyValue helpLuckyValue = iHelpLuckyValueService.selectByUserId(userId);
        LuckyValueQueue luckyValueQueue = iLuckyValueQueueService.selectLuckyValueQueueByUserId(userId);
        Map<String,Object> map=new HashMap<>();
        if(luckyValueQueue==null){
            List<LuckyQueue> luckyQueues = luckyQueueService.selectLuckyQueueListAsc();
            map.put("asRequired",luckyQueues.get(0).getLuckyValue());
        }else{
            LuckyQueue luckyQueue = luckyQueueService.selectLuckyQueueById(luckyValueQueue.getLuckyQueueId());
            map.put("asRequired",luckyQueue.getLuckyValue());
        }
        map.put("helpLuckyValue",helpLuckyValue);

        return ResultUtil.success(map);
    }

    @RequestMapping(value = "/selectLuckyDetails", method = RequestMethod.GET)
    @ApiOperation("查看我的幸运值详细信息")
    @ResponseBody
    public Result selectLuckyDetails(int page,int size,Long userId) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(luckyDetailsService.selectHelpLuckyDetailsListByUserId(pageable,userId));
    }

    @RequestMapping(value = "/selectLuckyQueueList", method = RequestMethod.GET)
    @ApiOperation("查看所有的队列列表")
    @ResponseBody
    public Result selectLuckyQueueList() {
        return ResultUtil.success(luckyQueueService.selectLuckyQueueListAsc());
    }

    @RequestMapping(value = "/selectLuckyQueueDetails", method = RequestMethod.GET)
    @ApiOperation("查看队列榜信息")
    @ResponseBody
    public Result selectLuckyQueueDetails(Long userId) {
        Map<String,Object> map=new HashMap<>();
        LuckyValueQueue luckyValueQueue = iLuckyValueQueueService.selectLuckyValueQueueByUserId(userId);
        UserInfo userByWxId = userJpaRepository.findById(userId).orElse(null);
        if(luckyValueQueue==null){
            map.put("flag",1);
            HelpLuckyValue helpLuckyValue = iHelpLuckyValueService.selectByUserId(userId);
            map.put("user",userByWxId);
            map.put("luckyValue",helpLuckyValue.getLuckyValue());
            map.put("upgrade",iHelpGetTaskeSrvice.updateLuckys(userByWxId));
            return ResultUtil.success(map);
        }
        List<Map<String, Object>> maps = iLuckyValueQueueService.selectLuckQueueLocation(luckyValueQueue.getLuckyQueueId());
        Map<String, Object> stringObjectMap = iLuckyValueQueueService.selectLuckQueueLocationByUserIds(userByWxId.getRelationId(),userId, luckyValueQueue.getLuckyQueueId());
        map.put("flag",2);
        map.put("userList",maps);
        map.put("user",stringObjectMap);
        map.put("upgrade",iHelpGetTaskeSrvice.updateLuckys(userByWxId));
        return ResultUtil.success(map);
    }


    @RequestMapping(value = "/selectLuckyQueueRanking", method = RequestMethod.GET)
    @ApiOperation("管理平台查看红包榜排名")
    @ResponseBody
    public Result selectLuckyQueueRanking(int page,int size,String phone,Long id) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iHelpLuckyValueService.selectLuckyQueueRanking(pageable,id,phone));
    }



    @RequestMapping(value = "/selectLuckyQueueByUser", method = RequestMethod.GET)
    @ApiOperation("查看我邀请的好友详细信息")
    @ResponseBody
    public Result selectLuckyQueueByUser(int page,int size,Long userId) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        UserInfo userByWxId = userJpaRepository.findById(userId).orElse(null);
        Page<Map<String, Object>> maps = iLuckyValueQueueService.selectLuckyQueueByUser(pageable, userByWxId.getRelationId());
       /* Map<String,Object> map=new HashMap<>();
        map.put("user",userByWxId);
        map.put("userList",maps);*/
        return ResultUtil.success(maps);
    }


    @RequestMapping(value = "/updateLucky", method = RequestMethod.POST)
    @ApiOperation("申请晋级")
    @ResponseBody
    public Result auditHelpGetTask(Long userId) {
        UserInfo userByWxId = userJpaRepository.findById(userId).orElse(null);
        if(userByWxId!=null){
            int i = iHelpGetTaskeSrvice.updateLucky(userByWxId);
            if(i==0){
                return ResultUtil.error(-100,"晋级失败，条件不满足");
            }
            return ResultUtil.success("晋级成功");
        }
        return ResultUtil.error(-100,"晋级失败，账户信息错误");

    }











}